import config from "@/config/index.js";
import CryptoJS from "crypto-js";

const tools = {
    // LocalStorage 存储
    data: {
        set: (key, data) => {
            localStorage.setItem(key, JSON.stringify(data))
        },
        get: (key) => {
            const data = localStorage.getItem(key);
            return JSON.parse(data)
        },
        contain(key){
            try {
                let data = localStorage.getItem(key)
                if(data == null || Object.keys(data).length <= 0){
                    return false
                }
                return true
            }catch (err){
                return null
            }
        },
        remove(key) {
            localStorage.removeItem(key)
        },
        clear() {
            localStorage.clear()
        }
    },
    // SessionStorage 存储
    session: {
        set: (key, data) => {
            sessionStorage.setItem(key, JSON.stringify(data))
        },
        get: (key) => {
            const data = sessionStorage.getItem(key)
            return JSON.parse(data)
        },
        contain(key){
            try {
                let data = sessionStorage.getItem(key)
                if(data == null || Object.keys(data).length <= 0){
                    return false
                }
                return true
            }catch (err){
                return null
            }
        },
        remove(key) {
            sessionStorage.removeItem(key)
        },
        clear() {
            sessionStorage.clear()
        }
    },
    object: {
        copy: (obj) => {
            return JSON.parse(JSON.stringify(obj))
        },
        clone: (source) => {
            if (!source && typeof source !== "object") {
                console.warn("[Dreamer Error]: 源对象为空，深度克隆失败。")
            }
            const targetObj = source.constructor === Array ? [] : {}
            Object.keys(source).forEach((keys) => {
                if (source[keys] && typeof source[keys] === "object") {
                    targetObj[keys] = clone(source[keys])
                } else {
                    targetObj[keys] = source[keys]
                }
            })
            return targetObj
        }
    },
    date: {
        format: (date, fmt = "yyyy-MM-dd hh:mm:ss") => {
            date = new Date(date)
            var o = {
                "M+": date.getMonth() + 1, //月份
                "d+": date.getDate(), //日
                "h+": date.getHours(), //小时
                "m+": date.getMinutes(), //分
                "s+": date.getSeconds(), //秒
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                S: date.getMilliseconds(), //毫秒
            }
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length))
                }
            }
            return fmt
        }
    },
    screen: {
        toggle(element){
            var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement);
            if (isFull) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.webkitExitFullscreen) {
                    document.webkitExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.msRequestFullscreen) {
                    element.msRequestFullscreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.webkitRequestFullscreen) {
                    element.webkitRequestFullscreen();
                }
            }
            return !isFull;
        }
    },
    crypto: {
        // MD5加密
        MD5(data) {
            return CryptoJS.MD5(data).toString()
        },
        // BASE64加解密
        BASE64: {
            encrypt(data) {
                return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data))
            },
            decrypt(cipher) {
                return CryptoJS.enc.Base64.parse(cipher).toString(CryptoJS.enc.Utf8)
            }
        },
        // AES加解密
        AES: {
            encrypt: (data) => {
                if (config.crypto.secretKey.length % 8 != 0) {
                    console.warn("[Dreamer Error]: 秘钥长度需为8的倍数，否则解密将会失败。")
                }
                const result = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(config.crypto.secretKey), {
                    iv: CryptoJS.enc.Utf8.parse(config.crypto.config.iv || ""),
                    mode: CryptoJS.mode[config.crypto.config.mode || "ECB"],
                    padding: CryptoJS.pad["Pkcs7"],
                })
                return result.toString()
            },
            decrypt: (cipher) => {
                const result = CryptoJS.AES.decrypt(cipher, CryptoJS.enc.Utf8.parse(config.crypto.secretKey), {
                    iv: CryptoJS.enc.Utf8.parse(config.crypto.config.iv || ""),
                    mode: CryptoJS.mode[config.crypto.config.mode || "ECB"],
                    padding: CryptoJS.pad["Pkcs7"],
                })
                return CryptoJS.enc.Utf8.stringify(result)
            }
        }
    }
}
export default tools